import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import Messaging.Hit;
import Messaging.IMessage;


public class GUI extends JPanel
implements ActionListener{
	private ArrayBlockingQueue<IMessage> predatorCommands;
	private ArrayBlockingQueue<IMessage> preyCommands;
	private ArrayBlockingQueue<Hit> hitQueue;
    private JProgressBar predLifeBar;
    private JProgressBar predStamBar;
    private JProgressBar preyLifeBar;
    private JProgressBar preyStamBar;
    private JButton predConnectButton;
    private JButton preyConnectButton;
    private JLabel preyName;
    private JLabel predName;
    private JLabel preyNameLabel;
    private JLabel predNameLabel;
    private CommunicateThread predComm;
    private CommunicateThread preyComm;
    private HitControl hitControl;
    
    
    ActionMap actionMap;
    InputMap inputMap;
    
	
	public GUI()
	{
		super(new BorderLayout());
		 
        //Create the demo's UI.
        predConnectButton = new JButton("Connect");
        predConnectButton.setActionCommand("connectPred");
        predConnectButton.addActionListener(this);
        
        preyConnectButton = new JButton("Connect");
        preyConnectButton.setActionCommand("connectPrey");
        preyConnectButton.addActionListener(this);
        
        predLifeBar = new JProgressBar(0, 100);
        Color c = new Color (0, 198, 0);
        predLifeBar.setForeground(c);
        predLifeBar.setBackground(Color.white);
        predLifeBar.setValue(100);
        predLifeBar.setStringPainted(true);
        
        predStamBar = new JProgressBar(0, 100);
        c = new Color (0, 0, 198);
        predStamBar.setForeground(c);
        predStamBar.setBackground(Color.white);
        predStamBar.setValue(100);
        predStamBar.setStringPainted(true);
        
        preyLifeBar = new JProgressBar(0, 100);
        c = new Color (0, 198, 0);
        preyLifeBar.setForeground(c);
        preyLifeBar.setBackground(Color.white);
        preyLifeBar.setValue(100);
        preyLifeBar.setStringPainted(true);
        
        preyStamBar = new JProgressBar(0, 100);
        c = new Color (0, 0, 198);
        preyStamBar.setForeground(c);
        preyStamBar.setBackground(Color.white);
        preyStamBar.setValue(100);
        preyStamBar.setStringPainted(true);
        
        preyName = new JLabel("NAME", JLabel.CENTER);
        predName = new JLabel("NAME", JLabel.CENTER);
        preyName.setFont (preyName.getFont().deriveFont (35.0f));
        predName.setFont (predName.getFont().deriveFont (35.0f));
        
        preyNameLabel = new JLabel("PREY", JLabel.CENTER);
        predNameLabel = new JLabel("PREDATOR", JLabel.CENTER);
 
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 3));
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        
        panel.add(preyName);
        panel.add(new JLabel());
        panel.add(predName);
        
        panel.add(preyNameLabel);
        panel.add(new JLabel());
        panel.add(predNameLabel);
        
        panel.add(preyConnectButton);
        panel.add(new JLabel());
        panel.add(predConnectButton);
        
        JPanel midPanel1 = new JPanel();
        midPanel1.add(new JLabel("Life        ", JLabel.LEFT));
        midPanel1.add(preyLifeBar);
        panel.add(midPanel1);
        
        panel.add(new JLabel());
        
        JPanel midPanel3 = new JPanel();
        midPanel3.add(new JLabel("Life        ", JLabel.LEFT));
        midPanel3.add(predLifeBar);
        panel.add(midPanel3);
        
        JPanel midPanel2 = new JPanel();
        midPanel2.add(new JLabel("Stamina", JLabel.LEFT));      
        midPanel2.add(preyStamBar);
        panel.add(midPanel2);
        
        panel.add(new JLabel());
        
        JPanel midPanel4 = new JPanel();
        midPanel4.add(new JLabel("Stamina", JLabel.LEFT));      
        midPanel4.add(predStamBar);
        panel.add(midPanel4);
        
        add(panel, BorderLayout.PAGE_START);
        
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        actionMap = panel.getActionMap();
        int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
        inputMap = panel.getInputMap(condition);
        
		predatorCommands = new ArrayBlockingQueue<>(2); 
		preyCommands = new ArrayBlockingQueue<>(2); 
		hitQueue = new ArrayBlockingQueue<>(2);
		predComm = new CommunicateThread(predatorCommands, preyCommands,predName,predLifeBar,predStamBar,hitQueue,RobotNames.Predator,RobotNames.PredatorAddress);
		
		preyComm = new CommunicateThread(preyCommands,predatorCommands,preyName,preyLifeBar,preyStamBar,hitQueue,RobotNames.Prey,RobotNames.PreyAddress);
		

        hitControl = new HitControl(hitQueue,predatorCommands, preyCommands);
        hitControl.start();
        
        Key.KeyList = new ArrayList<>();
        
        setKeyMapping(new Key(predatorCommands), KeyEvent.VK_UP);
        setKeyMapping(new Key(predatorCommands), KeyEvent.VK_DOWN);
        setKeyMapping(new Key(predatorCommands), KeyEvent.VK_RIGHT);
        setKeyMapping(new Key(predatorCommands), KeyEvent.VK_LEFT);
        
	}
	
	private void setKeyMapping(Key key, int keyCode)
	{
		Key.KeyList.add(key);
		
        //The true parameter says that the keystroke represents a key release rather than a key press.
        inputMap.put(KeyStroke.getKeyStroke(keyCode, 0,true), "p" + keyCode);
        actionMap.put("p" + keyCode , new KeyUp(key));     
        
        inputMap.put(KeyStroke.getKeyStroke(keyCode, 0,false), "r" + keyCode);
        actionMap.put("r" + keyCode, new KeyDown(key)); 
	}
	/**
     * Create the GUI and show it. As with all GUI code, this must run
     * on the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Prey Escape");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Create and set up the content pane.
        JComponent newContentPane = new GUI();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
	
	public static void main(String[] args) {
		//Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == predConnectButton.getActionCommand())
		{
			predConnectButton.setEnabled(false);
			predConnectButton.setVisible(false);
	        predComm.execute();
		}

		else if(e.getActionCommand() == preyConnectButton.getActionCommand())
		{
			preyConnectButton.setEnabled(false);
			preyConnectButton.setVisible(false);
	        preyComm.execute();
		}
	}
	
	
	

}
